//
//  ModifyViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 13-5-7.
//  Copyright (c) 2013年 MEETStudio. All rights reserved.
//

#import "ModifyViewController.h"


static ASIHTTPConnection *modifyKey_connect;

@interface ModifyViewController ()

@end

@implementation ModifyViewController

- (void)dealloc{
    
    if (modifyKey_connect != nil) {
        modifyKey_connect.connectionDelegate = nil;
        [modifyKey_connect release];
        modifyKey_connect = nil
        ;
    }
    
    
    [nickNameTextField release];
    [oldKeyTextField release];
    [newKeyTextField release];
    
    
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIButton *rightBt = [[UIButton alloc]initWithFrame:CGRectMake(0, 7, 50, 30)];
    [rightBt.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [rightBt.titleLabel setTextColor:[UIColor whiteColor]];
    [rightBt setTitle:@"修改" forState:UIControlStateNormal];
    [rightBt addTarget:self action:@selector(modifyKey) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:rightBt];
    [self.navigationItem setRightBarButtonItem:rightButton];
    [rightBt release];
    [rightButton release];

    
    
}

- (void)modifyKey{
    
    NSString *userName = [[NSUserDefaults standardUserDefaults] valueForKey:USER_NAME];
    NSString *oldeKey = [[NSUserDefaults standardUserDefaults] valueForKey:USER_KEY];
    
    
    if (![oldeKey isEqualToString:oldKeyTextField.text] || ![userName isEqualToString:nickNameTextField.text]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:@"用户名密码不匹配"
                              
                                                       delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [alert show];
        [alert release];

        return;
    }
   
    
    [LoadingView showDefaultLoadingView];
    NSString *urlString = [NSString stringWithFormat:@"%@/MobileUser/UpdatePasswordAction.aspx?UserName=%@&newPassword=%@&oldPassword=%@",BASEURL,userName,newKeyTextField.text,oldeKey];;
   
    NSString *encodedValue = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
	NSURL *url = [NSURL URLWithString:encodedValue];
    

    
    if (modifyKey_connect != nil) {
        modifyKey_connect.connectionDelegate = nil;
        [modifyKey_connect release];
        modifyKey_connect = nil
        ;
    }
    
    modifyKey_connect = [[ASIHTTPConnection alloc] initGETWithURL:url];
    modifyKey_connect.connectionDelegate = self;
    
    [modifyKey_connect startAsynchronous];
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    [LoadingView showDurationMessageLoadingViewWith:1 message:@"网络不给力，稍后重试"];
}


- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    if (dict) {
        
        BOOL isSuccessed = [[dict objectForKey:@"isSuccess"] boolValue];
        if (isSuccessed) {
            [LoadingView showDurationMessageLoadingViewWith:2 message:@"修改成功"];
        }
        
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
